#include "netmain.h"
#include <QTcpSocket>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
NetMain *NetMain::obj = nullptr;

NetMain::NetMain(QObject *parent)
    : QObject{parent}
    , client(new QTcpSocket)
{
    connect(client, &QTcpSocket::readyRead,
            this, &NetMain::clientReadyReadSlot);
    connectToHost();
}



NetMain *NetMain::getObject()
{
    if(obj == nullptr)
        obj = new NetMain;
    return obj;
}

void NetMain::setCarAlarm(int carId, bool sw)
{
    connectToHost();
    QJsonObject obj;
    obj.insert("type",2003);
    obj.insert("car_id",carId);
    obj.insert("sw",sw);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::setCarAlarmLight(int carId, bool sw)
{
    connectToHost();
    QJsonObject obj;
    obj.insert("type",2004);
    obj.insert("car_id",carId);
    obj.insert("sw",sw);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::setCarLight(int carId, bool sw)
{
    connectToHost();
    QJsonObject obj;
    obj.insert("type",2002);
    obj.insert("car_id",carId);
    obj.insert("sw",sw);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::setCarStatus(int carId, bool sw)
{
    connectToHost();
    QJsonObject obj;
    obj.insert("type",2001);
    obj.insert("car_id",carId);
    obj.insert("sw",sw);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::setJunNode(int junId, int mode)
{
    connectToHost();
    QJsonObject obj;
    obj.insert("type",2005);
    obj.insert("junction_id",junId);
    obj.insert("mode",mode);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::clientReadyReadSlot()
{
    QByteArray data = client->readAll();
    int count = 0;  //当前括号数量
    int start = -1; //开始的索引
    for(int i = 0; i < data.length(); i++)
    {
        if(data.at(i) == '{')
        {
            count++;
            if(start == -1)
            {
                start = i;
            }
        }
        else if(data.at(i) == '}')
        {
            count--;
            if(count == 0)
            {
                QByteArray temp = data.mid(start, i-start+1);
                start = -1;
                QJsonObject obj = QJsonDocument::fromJson(temp).object();
                handleFrame(obj);
            }
        }
    }
}

void NetMain::connectToHost()
{
    if(client->state() == QTcpSocket::ConnectedState)
        return;
    client->connectToHost("127.0.0.1", 10010);
    bool ok =client->waitForConnected(1000);
    if(ok)
        sendIdentity();
}

void NetMain::sendIdentity()
{
    QJsonObject obj;
    obj.insert("type",5000);
    obj.insert("identity",0);
    client->write(QJsonDocument(obj).toJson());
}

void NetMain::handleFrame(const QJsonObject &obj)
{
    int type = obj.value("type").toInt();
    if(type == 1001)
    {
        //车辆位置
        handleCarPosFrame(obj);
    }
    else if (type == 1002) {
        handleLampFrame(obj);
    }
}

void NetMain::handleCarPosFrame(const QJsonObject &obj)
{
    //car_id round_id round_pos
    int carId = obj.value("car_id").toInt();
    int roundId = obj.value("round_id").toInt();
    int roundPos = obj.value("round_pos").toInt();
    emit newCarPos(carId, roundId, roundPos);
}

void NetMain::handleLampFrame(const QJsonObject &obj)
{
    int junctionId =obj.value("junction_id").toInt();
    QJsonArray lamp= obj.value("lamp").toArray();
    for(int i = 0;i<lamp.size(); i++)
    {
        int lampId=lamp.at(i).toObject().value("lamp_id").toInt();
        int color = lamp.at(i).toObject().value("color").toInt();
        emit newLampColor(junctionId,lampId,color);
    }
}





